/**
 * @Author：ming
 * @Date：2023/10/4 11:59 PM
 * @Filename：Lc20
 */

import java.util.Stack;

/**
 * @author ming
 */
public class Lc20 {
    public boolean isValid(String s) {
        char[] cc = s.toCharArray();
        Stack<Character> stack = new Stack<>();

        for (char c : cc) {
            if (c == ')') {
                if (stack.isEmpty()) {
                    return false;
                } else {
                    Character peek = stack.peek();
                    if (peek == '(') {
                        stack.pop();
                        continue;
                    } else {
                        return false;
                    }
                }
            } else if (c == '}') {
                if (stack.isEmpty()) {
                    return false;
                } else {
                    Character peek = stack.peek();
                    if (peek == '{') {
                        stack.pop();
                        continue;
                    } else {
                        return false;
                    }
                }
            }else if (c == ']') {
                if (stack.isEmpty()) {
                    return false;
                } else {
                    Character peek = stack.peek();
                    if (peek == '[') {
                        stack.pop();
                        continue;
                    } else {
                        return false;
                    }
                }
            }else {
                stack.push(c);
            }

        }

        if(stack.size()==0){
            return true;
        }else {
            return false;
        }

    }
}
